import msg from "../../utils/msg";
import { activity_detail, getGameDetail, joinGame, join_activity } from "../../utils/request/api";
import { Competition } from "../../utils/request/interface";
import user from "../../utils/user";
import User from "../../utils/user";

// pages/pay/pay.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    payType: 0,
    param: {
      name: '' as string,
      phone: '' as string,
    },
    type: 1,//1：赛事支付
    id: 0,
    com: {} as any,
    act:{} as any,
    //通用显示参数
    title: '',//上方标题
    typeTitle:'赛事名称',
    payment: 0,//支付金额
    payType_show: [],
    showWxPay: false,
    showOffLine: false,
    //
    join_msg:"*报名信息将公开显示，请填写合法合规内容",
  },
  onSubmit: async function () {
    //检查是否选择了支付方式
    if(this.data.payType==0){
      msg.show('请选择支付方式');return;
    }
    let user = new User;
    msg.Loading();
    let param = this.data.param;
    //加入活动
    //加入赛事
    let res: any;
    if(this.data.type==1){
      res = await joinGame({ GF_ID: user.GF_ID, game_id: this.data.com.id, game_data_id: this.data.com.game_data[0].id, team_logo: user.head_pic, team_name: param.name,tour_leader_phone:param.phone,tour_leader_name:param.name });
    }
    else if(this.data.type==2){
      res = await join_activity({GF_ID: user.GF_ID,activity_id:this.data.act.acitivty_data_id});
    }
    msg.hideLoading();
    if (res.data.error == 0) {
      this.setData({ has_joined: true });
      let temp_msg = '';
      if(this.data.payType==1) temp_msg = '请线下向商家付款';
      await msg.show('报名成功',temp_msg);
      wx.navigateBack();
    }
    else msg.show('报名失败', res.data.msg||'');
  },
  bindInput(e: any) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      ['param.' + name]: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    wx.setNavigationBarTitle({
      title: '支付',
    });

    if (!options.id || !options.type) { wx.navigateBack(); return; }
    this.getData(options);
    this.init(options);
  },
  getData(options: any) {
    let user = new User;
    this.setData({
      ['param.name']: user.name,
      ['param.phone']: user.phone,
      type: options.type,
      id: options.id,
    });
  },
  init: async function (options: any) {
    let type = options.type;
    let id = options.id;
    if (type == 1) {//赛事
      this.comInit(id); return;
    }
    else if(type == 2){//活动
      this.actInit(id);
    }
  },
  //活动初始化
  actInit:async function (id: string){
    wx.setNavigationBarTitle({
      title: '活动报名支付',
    });
    let user = new User;
    msg.Loading('订单初始化中');
    let res:any = await activity_detail({ activity_id: id, GF_ID: user.GF_ID.toString() });
    if (res.data.error != 0) {//获取失败
      msg.hideLoading();
      await msg.show('活动详情获取失败');
      wx.navigateBack();
      return;
    }
    let act = res.data.datas;
    this.setData({
      act: act,
      title: act.activity_title,
      payment: Number(act.max_price) * 100,
      loading: false,
      typeTitle:'活动名称',
    });
    //展示支付方式，活动根据isSignOnline属性，642-线上，641-线下
    if (act.issignonline == 642) {
      this.setData({
        showWxPay: true,
        payType:2,
      });
    }
    else {
      this.setData({
        showOffLine: true,
        payType:1,
      });
    }
    msg.hideLoading();
  },
  //赛事初始化
  comInit: async function (id: string | number) {
    wx.setNavigationBarTitle({
      title: '赛事报名支付',
    });
    let user = new User;
    msg.Loading('订单初始化中');
    let res: any = await getGameDetail({ GF_ID: user.GF_ID, game_id: id, AccessKeyId: user.AccessKey.AccessKeyId, AccessKeySecret: user.AccessKey.AccessKeySecret });
    if (res.data.error != 0) {//获取失败
      msg.hideLoading();
      await msg.show('赛事详情获取失败');
      wx.navigateBack();
      return;
    }
    let com = res.data.datas;
    this.setData({
      com: com,
      title: com.game_title,
      payment: Number(com.game_data[0].game_money) * 100,
      loading: false,
    });
    //展示支付方式，赛事根据isSignOnline属性，642-线上，641-线下
    if (com.game_data[0].isSignOnline == 642) {
      this.setData({
        showWxPay: true,
        payType:2,
      });
    }
    else {
      this.setData({
        showOffLine: true,
        payType:1,
      });
    }
    msg.hideLoading();
  },
  check(e: any) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      payType: type
    })
  },
  Ucheck() {
    this.setData({
      payType: 0
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})